Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: make mcap-rs wasm compatible #989

Merged
merged 5 commits into from
Oct 31, 2023
Merged

Rust: make mcap-rs wasm compatible #989

merged 5 commits into from
Oct 31, 2023

Conversation

JiangengDong
Copy link
Contributor

@JiangengDong JiangengDong commented Oct 6, 2023

Public-Facing Changes

  • Dependency is changed from lz4 to lz4_flex
  • A new feature flag lz4 is introduced and enabled by default

Description

I am trying to use mcap-rs in an application that targets multiple platforms, including native and wasm. I modified the source code to make it wasm compatible, and think this may be also useful for others.

The changes include:

  • Replace lz4 with lz4_flex, because the former one depends on lz4-sys, which is not easy to port to wasm.
  • Add a feature flag lz4 so the library user can choose to opt-out of the dependency.
  • Turn off the default features and turn on the wasm feature for zstd when the target platform is wasm.
  • Add Clippy checking in the CI for all combinations of features.
  • Minor fixes for Clippy linter warnings.

I am not quite sure if it would be a good idea to change the lz4 library. It is also possible to still use lz4 library and opt-out of it when the platform is wasm.

I haven't raised an issue yet, because I am not sure if this belongs to a bug fix or feature request. (Feature request always leads me to the "discussion" page. Is that the correct place?) Please let me know where I should create it.

@CLAassistant
Copy link

CLAassistant commented Oct 6, 2023

CLA assistant check
All committers have signed the CLA.

@defunctzombie
Copy link
Contributor

Thanks for opening this effort and the writeup; this is definitely a feature that I think makes the rust library more desirable and we should add this capability.

Sharing some of my thoughts:

  • A new feature flag lz4 is introduced and enabled by default

Is there a feature flag for zstd? If not - why does only lz4 get a feature flag?

I am not quite sure if it would be a good idea to change the lz4 library.

If the new library supports writing and reading lz4 in a way that the existing library also supports then I don't see a problem. I don't know if anything specific went into picking lz4 over the other options when the rust code was first added.

  • Turn off the default features and turn on the wasm feature for zstd when the target platform is wasm.

Do we need to turn off the default features? It isn't clear to me if turning off the default is a requirement to turning wasm on.

cc @mrkline

@JiangengDong
Copy link
Contributor Author

Is there a feature flag for zstd?

Yes, and it is already on main.

zstd = ["dep:zstd"]

If the new library supports writing and reading lz4 in a way that the existing library also supports then I don't see a problem.

I generated several mcap files and tried without a problem. Let me know if we should add a test for this. lz4 is still the most popular lz4 compression library in Rust, and I think that's why it was chosen at the beginning.

Do we need to turn off the default features? (zstd)

Good point. I copied the code from another post and did not verify by myself. Just tried again and confirmed we don't need to turn off the default features. Pushed a commit to fix this.

Copy link
Contributor

@mrkline mrkline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

As a nit I'd say that it would be nicer to have cleanup of unrelated code (e.g., reformatting, self: &Self -> &self, etc.) as a seperate PR, but wider support for compression is great in my book!

FWIW, I don't think there was any deep reason for chosing lz4 other than that it's a popular crate. 😅

rust/Cargo.toml Outdated
Comment on lines 5 to 6
keywords = ["foxglove", "mcap"]
categories = ["science::robotics", "compression"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this some auto-formatter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I can revert it.

Comment on lines +27 to +30
[target.'cfg(target_arch = "wasm32")'.dependencies]
zstd = { version = "0.11", features = ["wasm"], optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice - appreciate you only pulling in wasm support when it's the target!

@JiangengDong
Copy link
Contributor Author

Pushed two commits to revert all the irrelevant changes made by the linter and formatter. Let me know if this looks good to you.

@defunctzombie defunctzombie merged commit 6fd068f into foxglove:main Oct 31, 2023
25 checks passed
@JiangengDong JiangengDong deleted the jiangeng/rust-wasm-compatibility branch October 31, 2023 05:51
pezy pushed a commit to pezy/mcap that referenced this pull request Jan 11, 2024
### Public-Facing Changes

* Dependency is changed from
[lz4](https://github.com/10xGenomics/lz4-rs) to
[lz4_flex](https://github.com/PSeitz/lz4_flex)
* A new feature flag `lz4` is introduced and enabled by default

### Description
This change makes mcap-rs wasm compatible.

* Replace [lz4](https://github.com/10xGenomics/lz4-rs) with
[lz4_flex](https://github.com/PSeitz/lz4_flex), because the former one
depends on `lz4-sys`, which is not easy to port to wasm.
* Add a feature flag `lz4` so the library user can choose to opt-out of
the dependency.
* ~~Turn off the default features and~~ turn on the `wasm` feature for
[zstd](https://github.com/gyscos/zstd-rs) when the target platform is
wasm.
* Add Clippy checking in the CI for all combinations of features.
* Minor fixes for Clippy linter warnings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants